#include "stdafx.h"
#include "CSProto.h"
#include "Session.h"


void DecodeCS_MESSAGE_REQUEST_ID(const void * buf, int nSize, CSession * pProc)
{
    MESSAGE pkg;
    if (!pkg.ParseFromArray(buf, nSize))
        return;
    if (pProc){
        pProc->OnRecvMessageReq(&pkg);
    }
}

void DecodeCS_MESSAGE_BROADCAST_ID(const void * buf, int nSize, CSession * pProc)
{
}

void DecodeCS_CHATROOM_RESPONSE_ID(const void * buf, int nSize, CSession * pProc)
{
}

void DecodeCS_CHATROOM_REQUEST_ID(const void * buf, int nSize, CSession * pProc)
{
    CHATROOM_REQUEST pkg;
    if (!pkg.ParseFromArray(buf, nSize))
        return;
    if (pProc){
        pProc->OnRecvChatRoomReq(&pkg);
    }
}

void DecodeCS_CHATROOM_UPDATE_ID(const void * buf, int nSize, CSession * pProc)
{
}

void DecodeCS_MESSAGE_RESPONSE_ID(const void * buf, int nSize, CSession * pProc)
{
}

void DecodeCS_CHATROOM_SETTING_ID(const void * buf, int nSize, CSession * pProc)
{
    CHATROOM_SETTING pkg;
    if (!pkg.ParseFromArray(buf, nSize))
        return;
    if (pProc){
        pProc->OnRecvRoomConf(&pkg);
    }
}

CCSProto::CCSProto()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    memset(m_DecodeFuncArray, 0, sizeof(m_DecodeFuncArray));
    Init();
}

CCSProto::~CCSProto()
{
    google::protobuf::ShutdownProtobufLibrary();
}

bool CCSProto::Init()
{
    m_DecodeFuncArray[CS_MESSAGE_REQUEST_ID] = &DecodeCS_MESSAGE_REQUEST_ID;
    m_DecodeFuncArray[CS_MESSAGE_BROADCAST_ID] = &DecodeCS_MESSAGE_BROADCAST_ID;
    m_DecodeFuncArray[CS_CHATROOM_RESPONSE_ID] = &DecodeCS_CHATROOM_RESPONSE_ID;
    m_DecodeFuncArray[CS_CHATROOM_REQUEST_ID] = &DecodeCS_CHATROOM_REQUEST_ID;
    m_DecodeFuncArray[CS_CHATROOM_UPDATE_ID] = &DecodeCS_CHATROOM_UPDATE_ID;
    m_DecodeFuncArray[CS_MESSAGE_RESPONSE_ID] = &DecodeCS_MESSAGE_RESPONSE_ID;
    m_DecodeFuncArray[CS_CHATROOM_SETTING_ID] = &DecodeCS_CHATROOM_SETTING_ID;
    return true;
}

void CCSProto::Decode(int nMsgID, const void * buf, int nSize, CSession * pProc)
{
    if(nMsgID > CSProtoMaxMessageID || m_DecodeFuncArray[nMsgID] == NULL){
        return;
    }
    m_DecodeFuncArray[nMsgID](buf, nSize, pProc);
}

